//package com.example.activiti.controller;
//
//import org.activiti.bpmn.model.BpmnModel;
//import org.activiti.bpmn.model.FlowElement;
//import org.activiti.bpmn.model.FlowNode;
//import org.activiti.bpmn.model.SequenceFlow;
//import org.activiti.engine.ActivitiException;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.impl.cmd.NeedsActiveTaskCmd;
//import org.activiti.engine.impl.interceptor.CommandContext;
//import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
//import org.activiti.engine.impl.persistence.entity.TaskEntity;
//import org.activiti.engine.impl.persistence.entity.TaskEntityManager;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.List;
//
//@Component
//public class RollbackFirstTaskCmd extends NeedsActiveTaskCmd<Void> {
//    private static Logger logger = LoggerFactory.getLogger(RollbackFirstTaskCmd.class);
//
//    public RollbackFirstTaskCmd(String taskId){
//        super(taskId);
//    }
//    public String deletereason;
//
//    @Resource
//    private RepositoryService repositoryService;
//
//    @Override
//    public Void execute(CommandContext commandContext, TaskEntity currentTask) {
//        String processDefinitionId = currentTask.getProcessDefinitionId();
//        String executionId = currentTask.getExecutionId();
//
//        TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
//        taskEntityManager.deleteTask(currentTask, deletereason, false);
//
//        FlowNode firstUserTask = this.findFirstActivity(processDefinitionId);
//        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findExecutionById(executionId);
//
//        // 获取目标节点的来源连线
//        List<SequenceFlow> flows = firstUserTask.getIncomingFlows();
//        if (flows == null || flows.isEmpty()) {
//            throw new ActivitiException("回退错误，目标节点没有来源连线");
//        }
//        // 随便选一条连线来执行，时当前执行计划为，从连线流转到目标节点，实现跳转
////        executionEntity.setCurrentFlowElement(flows.get(0));
////        commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
//
//        // executionEntity.setCurrentFlowElement(flowElement);
//        // commandContext.getAgenda().planContinueProcessOperation(executionEntity);
//
//        return null;
//    }
//
//    @Override
//    public String getSuspendedTaskException() {
//        return "挂起的任务不能跳转";
//    }
//
//    /**
//     * 获得第一个节点.
//     */
//    public FlowNode findFirstActivity(String processDefinitionId) {
////        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).latestVersion().singleResult();
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
//        FlowElement flowElement = (FlowElement) bpmnModel.getMainProcess().getFlowElements().toArray()[0];
////        Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
////        FlowElement flowElement = process.getInitialFlowElement();
//        FlowNode startActivity = (FlowNode) flowElement;
//
//        if (startActivity.getOutgoingFlows().size() != 1) {
//            throw new IllegalStateException(
//                    "start activity outgoing transitions cannot more than 1, now is : "
//                            + startActivity.getOutgoingFlows().size());
//        }
//
//        SequenceFlow sequenceFlow = startActivity.getOutgoingFlows().get(0);
////        FlowNode targetActivity = sequenceFlow.getTargetFlowElement();
////
////        if (!(targetActivity instanceof UserTask)) {
////            logger.info("first activity is not userTask, just skip");
////
////            return null;
////        }
//
//        return startActivity;
//    }
//}
